<template>
  <uni-pop type="confirm"
    v-model="popData.isShow"
    :title="type==1?'取消完成理由':'协商完成理由'"
    :showClose="true"
    class="pop-box"
    @input="onCancel"
  >
    <template slot="content">
      <view class="cont-box">
        <view>
          <textarea v-model="reason" placeholder="请输入理由" :maxlength="200" cursor-spacing="70" :show-confirm-bar="false"/>
        </view>
      </view>
    </template>
    <template slot="commit">
        <view class="btn-link btn-pop" @click="onConfirmSelect">
          保存
        </view>
      </template>
  </uni-pop>
</template>
<script>

export default {
  props: {
    orderId:{
      type: [Number,String],
      default:''
    },
    type:{ // 1取消,2协商
      type:Number,
      default:1
    }
  },
  watch:{
    orderId:{
      handler(newVal){
        if(newVal) {
          this.reason= ''
        }
      },
      deep:true,
      immediate: true
    }
  },
  data() {
    return {
      popData:{
        isShow:false
      }, // 弹窗信息
      reason:''
    }
  },
  methods: {
    showPicker(){
      this.popData.isShow = true
    },
    onCancel(){
      this.reason = ''
      this.popData.isShow = false
    },
    //确认支付
    onConfirmSelect() {
      if(!this.reason){
        this.$api.msg(`请输入${this.type==1?'取消完成':'协商完成'}理由`)
        return
      }
      if(this.type==1){
        this.postUpdate()
      } else {
        this.commitConsult()
      }
    },
    postUpdate(){
      const path = '/order/finishOrder'
      this.$apis.postApi(path,{orderId:this.orderId,status:16,operationReason:this.reason}).then(({code,message}) => {
        if(code==200){
          this.$api.msg('提交成功');
          this.$emit('onConfirm',true)
          this.onCancel()
        } else {
          this.$api.msg(message);
        }
      }).catch(err=>{
        console.log(err)
      })
    },
    commitConsult(){
      uni.showModal({
        title:'是否结束此订单',
        content: '维修材料费用跟师傅协商线下处理，后续保修维护直接由师傅来负责，平台仅收取上门服务费',
        success: (e)=>{
          if(e.confirm){
            this.$apis.postApi('/order/cancelOrderWithoutRefund',{orderId:this.orderId,operationReason:this.reason}).then(({code,message}) => {
              if(code==200){
                this.$api.msg('提交成功');
                this.$emit('onConfirm',true)
                this.onCancel()
              } else {
                this.$api.msg(message);
              }
            }).catch(err=>{
              console.log(err)
            })
          } else {
            this.onCancel()
          }
        }
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.cont-box{
  textarea{
    height: 140upx;
    width: calc(100% - 50upx);
    font-size: 34upx;
    text-align: left;
    margin: 40upx 0 20upx;
    padding: 25upx 25upx;
    background: #f5f5f5;
    font-size: 32upx;
    border-radius: 6upx;
  }
  
}
.btn-pop{
  width: 420upx;
}
</style>